﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace IupNET
{
    /// <summary>
    /// This image wraps the Image, ImageRGB and ImageRGBA classes
    /// </summary>
    public class IupImage:IupBase
    {

        
        public IupImage(string filename):base(IntPtr.Zero)
        {

            IntPtr img = Iupc.LoadImage(filename);
            if (img == IntPtr.Zero)
                throw new Exception("Failed to load image " + filename);
            Initialize(img);
        }

              
        /// <summary>
        /// For 8 bit images, this function sets entries in the palette.
        /// The indices can range from 0 to 255. The total number of colors is limited to 256 colors. 
        /// The values are integer numbers from 0 to 255. If the value of a given index is same as "BgColor", 
        /// the color used will be the background color of the element on which the image will be inserted. 
        /// The "BgColor" value must be defined within an index less than 16.
        /// </summary>
        public void SetPalette(int index, Color c)
        {
            if (index < 0 || index > 255)
                return;
            SetAttribute(index.ToString(), Convert.ColorToString(c));
        }

        public Color GetPalette(int index)
        {
            if (index < 0 || index > 255)
                return Color.Empty;
            string col=GetAttribute(index.ToString());
            return Convert.StringToColor(col);
        }

        /// <summary>
        /// Gets the number of bits per pixel in the image. 
        /// This value is 8, 24 or 32 .
        /// </summary>
        public int BPP
        {
            get
            {
                return Convert.StringToInt(GetAttribute("BPP"));
            }
        }


        /// <summary>
        /// Gets or sets the color used for transparency. 
        /// If not defined uses the BgColor of the control that contains the image.
        /// </summary>
        public Color BgColor
        {
            get
            {
                return Convert.StringToColor(GetAttribute("BGCOLOR"));
            }
            set
            {
                SetAttribute("BGCOLOR", Convert.ColorToString(value));
            }
        }

        /// <summary>
        /// Gets the number of channels in the image. 
        /// This value is 1,3 or 4 depending on how the image is created.
        /// </summary>
        public int Channels
        {
            get
            {
                return Convert.StringToInt(GetAttribute("CHANNELS"));
            }
        }

        /// <summary>
        /// Gets the image height in pixels.
        /// </summary>
        public int Height
        {
            get
            {
                return Convert.StringToInt(GetAttribute("HEIGHT"));
            }
        }

        /// <summary>
        /// Gets the image width in pixels.
        /// </summary>
        public int Width
        {
            get
            {
                return Convert.StringToInt(GetAttribute("WIDTH"));
            }
        }

        /// <summary>
        /// Gets or sets the Hotspot point, which is the position inside a cursor image indicating the mouse-click spot. 
        /// Its value is given by the x and y coordinates inside a cursor image. Default is 0,0 .
        /// </summary>
        public Point Hotspot
        {
            get
            {
                return Convert.StringToPoint(GetAttribute("HOTSPOT"));
            }

            set
            {
                SetAttribute("HOTSPOT", Convert.PointToString(value,":"));
            }
        }

        /// <summary>
        /// Gets the image size in pixels.
        /// </summary>
        public Size RasterSize
        {
            get
            {
                return Convert.StringToSize(GetAttribute("RASTERSIZE"));
            }
        }

        /// <summary>
        /// Gets the internal pixels data pointer.
        /// </summary>
        public IntPtr WID
        {
            get
            {
                return Iupc.GetAttributePtr(cobject, "WID");
            }

        }
        
    }
}
